package com.ideal.manage.esms.controller.esms;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ideal.manage.esms.bean.DTO.EsmsDTO;
import com.ideal.manage.esms.service.diagnostic.DiagnoseApiService;
import com.ideal.manage.esms.service.esms.EsmsDataService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * Created By Dai On 2018/5/8
 * Remark: 接口
 */
@Controller
@RequestMapping("esms")
public class EsmsController {
    private Logger logger = LoggerFactory.getLogger(EsmsController.class);
    private Logger apiLogger = LoggerFactory.getLogger("ESMSAPI");

    @Autowired
    private EsmsDataService esmsDataService;
    @Autowired
    private DiagnoseApiService diagnoseApiService;

    /**
     * Modbus协议上传接口
     */
    @RequestMapping(value = "mdbs")
    @ResponseBody
    public JSONObject getMdbs(@RequestBody EsmsDTO esms) {
        String projectId = esms.getProjectId();
        String data = esms.getData();
        apiLogger.info("msdb " + projectId + " data " + data);
        JSONObject result = new JSONObject();
        try {
            JSONArray jsonArray = JSONArray.parseArray(data);
            esmsDataService.batchInsertMDBS(projectId, jsonArray);
            result.put("code", 200);
            result.put("message", "调用成功");
        } catch (Exception e) {
            apiLogger.error("执行失败", e);
            result.put("code", 203);
            result.put("message", "未知错误");
        }
        return result;
    }

    /**
     * CAN报文解析上传接口
     */
    @RequestMapping("canbs")
    @ResponseBody
    public JSONObject getCanbs(@RequestBody EsmsDTO esms) {

        String projectId = esms.getProjectId();
        String data = esms.getData();
        apiLogger.info("canbs " + projectId + " data " + data);
        JSONObject result = new JSONObject();
        try {
            JSONArray jsonArray = JSONArray.parseArray(data);
            esmsDataService.batchInsertCANBS(projectId, jsonArray);
            result.put("code", 200);
            result.put("message", "调用成功");
        } catch (Exception e) {
            apiLogger.error("执行失败", e);
            result.put("code", 203);
            result.put("message", "未知错误");
        }
        return result;
    }

    @RequestMapping(value = "python1", method = RequestMethod.POST)
    @ResponseBody
    public JSONObject python1(@RequestBody EsmsDTO esms) {
        Long type = esms.getType();
        String data = esms.getData();
        JSONObject result = new JSONObject();
        try {
            JSONObject jsonData = JSONObject.parseObject(data);
            JSONArray jsonArray = diagnoseApiService.type1Read(jsonData);
            result.put("code", 200);
            result.put("type", type);
            result.put("result", jsonArray);
        } catch (Exception e) {
            logger.error("执行失败", e);
            result.put("code", 203);
            result.put("message", "未知错误");
        }
        logger.debug("type1 for read result :" + result);
        return result;
    }


    @RequestMapping(value = "algorithm/python1", method = RequestMethod.POST)
    @ResponseBody
    public JSONObject algorithmPython1(@RequestBody EsmsDTO esms) {
        Long type = esms.getType();
        String data = esms.getData();
        JSONObject result = new JSONObject();
        try {
            JSONObject jsonData = JSONObject.parseObject(data);
            diagnoseApiService.type1Write(jsonData);
            result.put("code", 200);
            result.put("type", type);
            result.put("message", "调用成功");
        } catch (Exception e) {
            logger.error("执行失败", e);
            result.put("code", 203);
            result.put("message", "未知错误");
        }
        logger.debug("type1 for write result :" + result);
        return result;
    }

    @RequestMapping(value = "python2", method = RequestMethod.POST)
    @ResponseBody
    public JSONObject python2(@RequestBody EsmsDTO esms) {
        Long type = esms.getType();
        String data = esms.getData();
        JSONObject result = new JSONObject();
        try {
            JSONObject jsonData = JSONObject.parseObject(data);
            JSONArray jsonArray = diagnoseApiService.type2Read(jsonData);
            result.put("type", type);
            result.put("code", 200);
            result.put("result", jsonArray);
        } catch (Exception e) {
            logger.error("执行失败", e);
            result.put("code", 203);
            result.put("message", "未知错误");
        }
        logger.debug("type2 for read result :" + result);
        return result;
    }

    @RequestMapping(value = "algorithm/python2", method = RequestMethod.POST)
    @ResponseBody
    public JSONObject algorithmPython2(@RequestBody EsmsDTO esms) {
        Long type = esms.getType();
        String data = esms.getData();
        JSONObject result = new JSONObject();

        try {
            JSONObject jsonData = JSONObject.parseObject(data);
            diagnoseApiService.type2Write(jsonData);
            result.put("type", type);
            result.put("code", 200);
            result.put("message", "调用成功");
        } catch (Exception e) {
            logger.error("执行失败", e);
            result.put("code", 203);
            result.put("message", "未知错误");
        }
        logger.debug("type2 for write result :" + result);
        return result;
    }


    @RequestMapping(value = "python3", method = RequestMethod.POST)
    @ResponseBody
    public JSONObject python3(@RequestBody EsmsDTO esms) {
        Long type = esms.getType();
        String data = esms.getData();
        JSONObject result = new JSONObject();
        try {
            JSONObject jsonData = JSONObject.parseObject(data);
            JSONArray jsonArray = diagnoseApiService.type3Read(jsonData);
            result.put("type", type);
            result.put("code", 200);
            result.put("result", jsonArray);
        } catch (Exception e) {
            logger.error("执行失败", e);
            result.put("code", 203);
            result.put("message", "未知错误");
        }
        logger.debug("type3 for read result :" + result);
        return result;
    }

    @RequestMapping(value = "algorithm/python3", method = RequestMethod.POST)
    @ResponseBody
    public JSONObject algorithmPython3(@RequestBody EsmsDTO esms) {
        Long type = esms.getType();
        String data = esms.getData();
        JSONObject result = new JSONObject();
        try {
            JSONObject jsonData = JSONObject.parseObject(data);
            diagnoseApiService.type3Write(jsonData);
            result.put("type", type);
            result.put("code", 200);
            result.put("message", "调用成功");
        } catch (Exception e) {
            logger.error("执行失败", e);
            result.put("code", 203);
            result.put("message", "未知错误");
        }
        logger.debug("type3 for write result :" + result);
        return result;
    }

    @RequestMapping(value = "python4", method = RequestMethod.POST)
    @ResponseBody
    public JSONObject python4(@RequestBody EsmsDTO esms) {

        Long type = esms.getType();
        String data = esms.getData();
        JSONObject result = new JSONObject();
        try {
            JSONObject jsonData = JSONObject.parseObject(data);
            JSONArray jsonArray = diagnoseApiService.type4Read(jsonData);
            result.put("type", type);
            result.put("code", 200);
            result.put("result", jsonArray);
        } catch (Exception e) {
            logger.error("执行失败", e);
            result.put("code", 203);
            result.put("message", "未知错误");
        }
        logger.debug("type4 for read result :" + result);
        return result;
    }

    @RequestMapping(value = "algorithm/python4", method = RequestMethod.POST)
    @ResponseBody
    public JSONObject algorithmPython4(@RequestBody EsmsDTO esms) {
        Long type = esms.getType();
        String data = esms.getData();
        JSONObject result = new JSONObject();
        try {
            JSONObject jsonData = JSONObject.parseObject(data);
            diagnoseApiService.type4Write(jsonData);
            result.put("type", type);
            result.put("code", 200);
            result.put("message", "调用成功");
        } catch (Exception e) {
            logger.error("执行失败", e);
            result.put("code", 203);
            result.put("message", "未知错误");
        }
        logger.debug("type4 for write result :" + result);
        return result;
    }

    @RequestMapping(value = "python5", method = RequestMethod.POST)
    @ResponseBody
    public JSONObject python5(@RequestBody EsmsDTO esms) {
        Long type = esms.getType();
        String data = esms.getData();
        JSONObject result = new JSONObject();
        try {
            JSONObject jsonData = JSONObject.parseObject(data);
            JSONArray jsonArray = diagnoseApiService.type5Read(jsonData);
            result.put("type", type);
            result.put("code", 200);
            result.put("result", jsonArray);
        } catch (Exception e) {
            logger.error("执行失败", e);
            result.put("code", 203);
            result.put("message", "未知错误");
        }
        logger.debug("type5 for read result :" + result);
        return result;
    }

    @RequestMapping(value = "algorithm/python5", method = RequestMethod.POST)
    @ResponseBody
    public JSONObject algorithmPython5(@RequestBody EsmsDTO esms) {
        Long type = esms.getType();
        String data = esms.getData();
        JSONObject result = new JSONObject();
        try {
            JSONObject jsonData = JSONObject.parseObject(data);
            diagnoseApiService.type5Write(jsonData);
            result.put("type", type);
            result.put("code", 200);
            result.put("message", "调用成功");
        } catch (Exception e) {
            logger.error("执行失败", e);
            result.put("code", 203);
            result.put("message", "未知错误");
        }
        logger.debug("type5 for write result :" + result);
        return result;
    }
}
